(in-package "ACL2")
(include-book "base-utils")
(include-book "../../include/src/xmlparser")
(include-book "../../include/src/io-utilities")
(include-book "data-structures/structures" :dir :system)


(defun ruleset? (form)
   (declare (ignore form))
    t)

(defstructure ruleset
    (rule-type)
    (death-states)
    (birth-states)
    (wolfram-code))

;;Given a list of characteres representing a ruleset
;;XML document, return a list of 2-element attribute/value lists
(defun get-tags* (xml)
  (parse-xml-doc xml)
  )

;;Given a list of tags (as formatted by get-tags*), return
;;the top-level tag specified by desired-tag. Top-level tags
;;are guarenteed uniqe by the xml standard of this project.
(defun get-toplevel-tag* (list-of-tags desired-tag)
  (if (not list-of-tags)
      "get-toplevel-tag*: Tag not found"
      (if (equal (str->chrs desired-tag) 
                 (str->chrs (car (car list-of-tags))))
          (car list-of-tags)
          (get-toplevel-tag* (cdr list-of-tags) desired-tag))))

;;Given a list of attribute/value state pairs, this function returns
;;a list of the values.
(defun get-states* (state-list)
  (if (not state-list)
      nil
      (cons (str->rat (car (cdr (car state-list))))
            (get-states* (cdr state-list)))))

(defun generate-lifelike-rules* (death-states birth-states) ;TODO swapped the order of death-states and birth-states in this "constructor" may need to update design doc
   (ruleset "lifelike" death-states birth-states nil))

(defun generate-elementary-rules* (wolfram-code)
    (ruleset "elementary" nil nil wolfram-code))

(defun alist->lifelike-rules* (alist-attribs)
   (declare (ignore alist-attribs))
    nil)

(defun alist->elementary-rules* (alist-attribs)
   (declare (ignore alist-attribs))
    nil)

;;Given a list of characters representing a ruleset
;;XML document, return the appropriate ruleset
(defun make-rules (xml-chrs)
    (let* ((tags (get-tags* xml-chrs))
          (ruletype (cadr (get-toplevel-tag* tags "ruleType"))))
      (cond ((string-equal "lifelike" ruletype)
              (generate-lifelike-rules*
                (get-states* 
                  (cadr (get-toplevel-tag* tags "deathStates")))
                (get-states* 
                  (cadr (get-toplevel-tag* tags "birthStates")))))
            ((string-equal "elementary" ruletype)
              (generate-elementary-rules*
                (str->rat 
                  (cadr (get-toplevel-tag* tags "wolframCode")))))
            (t
              nil))))